#include <Windowsx.h>
#include <stdio.h>
#include <vector>
#include "MessageCallback.h"
#include "Constants.h"
#include "resource.h"
#include "FileOpener.h"
#include "Settings.h"
#include "PluginLoader.h"
#include "SubtitleHandler.h"
#include "Exceptions.h"
#include "ErrorExit.h"
#include "Utilities.h"

using constants::INVISIBLE_COLOR;
using constants::WM_USER_NOTIF_ICON;

namespace SubPlayer
{
    LRESULT CALLBACK WindowProc(_In_  HWND hWnd, _In_  UINT uMsg, _In_  WPARAM wParam, _In_  LPARAM lParam)
    {
        try {
            PAINTSTRUCT ps;
            HDC hdc;
            static HFONT hFont = SetFont();
            static Settings settings;
            _wsetlocale(LC_ALL, settings.GetLocale().c_str());
            static SubtitleHandler subHandler(settings.GetDllLocation(), settings.GetEncoding(), hWnd); 
        

            switch (uMsg) {
            case WM_CREATE: 
            {  
                SetLayeredWindowAttributes(hWnd, INVISIBLE_COLOR, 255, LWA_COLORKEY);
                subHandler.StartPlaying();
            }
            break;
            case WM_PAINT:
            {
                hdc = BeginPaint(hWnd, &ps);

                wstring testStr = subHandler.GetCurrentSub();
                //:TODO remove
                SetBkMode(hdc, TRANSPARENT);
                TextOut(hdc, 5, 5, testStr.c_str(), testStr.size());

                EndPaint(hWnd, &ps);
            }
            break;
            case WM_COMMAND:
                switch LOWORD(wParam) {
                case IDM_OPEN_SUBTITLES:
                {
                    subHandler.LoadSubtitles();
                    break;
                }
                case IDM_SETTINGS:
                    break;
                case IDM_QUIT:
                    PostQuitMessage(0);
                    break;
                }
                break;
            case WM_DESTROY:
                PostQuitMessage(0);
                break;
            case WM_USER_NOTIF_ICON:
                switch (LOWORD(lParam)) {
                case WM_MBUTTONUP:
                    PostQuitMessage(0);
                    break;
                case WM_RBUTTONUP:
                    ShowMenu(hWnd, wParam);
                }
                break;
            default:
                return DefWindowProc(hWnd, uMsg, wParam, lParam);
            }
        }
        catch (Exceptions::SubtitleLoadException ex) {
            ErrorExit::ShowMessage(ex.what(), true);//ex.what(), true);
        }
        return 0;
    }

    void ShowMenu(HWND hWnd, WPARAM)
    {
        HINSTANCE hInstance = (HINSTANCE) GetWindowLong(hWnd, GWLP_HINSTANCE);
        HMENU notifMenu = LoadMenu(hInstance, L"NotificationMenu");
        HMENU notifMenuPopup = GetSubMenu(notifMenu, 0);
        POINT mousePos;
        if (!GetCursorPos(&mousePos)) {
            mousePos.x = 0;
            mousePos.y = 0;
        }
        TrackPopupMenu(notifMenuPopup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, mousePos.x, mousePos.y, 0, hWnd, NULL); 
        DestroyMenu(notifMenu);
    }

    HFONT SetFont()
    {

        return NULL;// CreateFont(
            //_In_  int nHeight, _In_  int nWidth, _In_  int nEscapement, _In_  int nOrientation, _In_  int fnWeight, _In_  DWORD fdwItalic, _In_  DWORD fdwUnderline,
            //    _In_  DWORD fdwStrikeOut, _In_  DWORD fdwCharSet, _In_  DWORD fdwOutputPrecision, _In_  DWORD fdwClipPrecision, _In_  DWORD fdwQuality, 
            //    _In_  DWORD fdwPitchAndFamily, _In_  LPCTSTR lpszFace);
    }
}